<!DOCTYPE html>
<html lang="en">
<head>
  <script type="text/javascript" src="js/jquery-3.7.0.min.js"></script>
  <meta charset="UTF-8">
  <link rel="stylesheet" href="css/style.css">
  <link rel="stylesheet" href="css/footer.css">
  <title>登录</title>
</head>
<body>
<div class="login-container">
  <h2>欢迎登录知识管理系统</h2>
  <form>
    <label for="username">用户名:</label>
    <input type="text" id="username" name="username" placeholder="请输入用户名" required>
    <div id="tip_uname">&nbsp;</div>
    <label for="password">密码:</label>
    <input type="password" id="password" name="password" placeholder="请输入密码" required>
    <div id="tip_pwd">&nbsp;</div>
    <button type="button" id="submit">登录</button>
    <div id="tip_submit">&nbsp;</div>
  </form>
  <a href="register.html">没有账号？去注册</a>
</div>
<div class="footer">
    软件21校企本1班 © 202124040126@王勤磊
</div>
<script>
  // 发送POST请求跳转到指定页面
  function httpPost(URL, PARAMS) {
    var temp = document.createElement("form");
    temp.action = URL;
    temp.method = "post";
    temp.style.display = "none";

    for (var x in PARAMS) {
      var opt = document.createElement("textarea");
      opt.name = x;
      opt.value = PARAMS[x];
      temp.appendChild(opt);
    }

    document.body.appendChild(temp);
    temp.submit();

    return temp;
  }

  $().ready(function (){
    $("#username").blur(function (){
      var xhr1 = new XMLHttpRequest();
      xhr1.onreadystatechange = function (){
        if(this.readyState===4){
          if (this.status===200) {
            $("#tip_uname").html(this.responseText)
          }
        }
      }
      xhr1.open("POST","checkUsernameLogin",false);
      xhr1.setRequestHeader("content-type","application/x-www-form-urlencoded")
      xhr1.send("username="+$("#username").val());
    })
    $("#password").blur(function (){
      var xhr2 = new XMLHttpRequest();
      xhr2.onreadystatechange = function (){
        if(this.readyState===4){
          if (this.status===200) {
            $("#tip_pwd").html(this.responseText)
          }
        }
      }
      xhr2.open("POST","checkPasswordLogin",false);
      xhr2.setRequestHeader("content-type","application/x-www-form-urlencoded")
      xhr2.send("password="+$("#password").val());
    })
    $("#submit").click(function (){
      $.ajax({
        type: "POST",
        url: "login",
        datatype: "json",
        data: "username="+$("#username").val()+"&password="+$("#password").val()+"&date="+new Date().getTime(),
        success: function (dataStr){
          data = JSON.parse(dataStr)
          alert(data.msg)
          if(data.code==0){
            var params = {
              username: data.obj.username,
              password: data.obj.password
            }
            httpPost("index.html", params);
          }else {
            $("#tip_submit").html("<font color=red>" + data.msg + "</font>")
          }
        }
      })
    })
    document.onkeydown= function (e) {
      var theEvent = window.event || e;
      var code = theEvent.keyCode || theEvent.which;
      if (code == 13) {
        $("#submit").click();
      }
    }

  })
</script>
</body>
</html>